﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Domain.Exceptions;

public static class ExceptionExtensions
{
    public static bool IsValidOrThrow(this List<ValidationError> errors)
    {
        if (errors == null || errors.Count == 0)
        {
            return true;
        }

        throw new DomainValidationException(errors);
    }

    public static void CheckRequired(this List<ValidationError> errors, object val, object displayName, string propName)
    {
        var isEmpty = false;

        if(val == null) {
            isEmpty = true;
        }
        else if ((val is string) && string.IsNullOrWhiteSpace(val as string))
        {
            isEmpty = true;
        }

        if (isEmpty)
        {
            errors.Add(new ValidationError("The field " + displayName + " is required.", propName));
        }
    }
}

